package com.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.easybuy.entily.Product;
import com.easybuy.service.ProductService;
import com.easybuy.serviceimpl.ProductServiceImpl;

public class ProductServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ProductServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		doPost(request,response);
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		ProductService ps=new ProductServiceImpl();
		String opr=request.getParameter("opr");
		if("list".equals(opr)){
			List<Product> li=ps.selAll();
			String json=JSON.toJSONString(li);
			out.print(json);
		}else if("del".equals(opr)){
			Product pro=new Product();
			pro.setId(Integer.valueOf(request.getParameter("id")));
			int res=ps.updateNewsInfo(pro);
			if(res==1){
				out.print("true");
			}else{
				out.print("false");
			}
		}else if("add".equals(opr)){
			Product pro=new Product();
			pro.setName(request.getParameter("name"));
			pro.setDescription(request.getParameter("description"));
			pro.setPrice(Integer.valueOf(request.getParameter("price")));
			pro.setStock(Integer.valueOf(request.getParameter("stock")));
			pro.setCategoryLevel1Id(Integer.valueOf(request.getParameter("categoryLevel1Id")));
			pro.setCategoryLevel2Id(Integer.valueOf(request.getParameter("categoryLevel2Id")));
			pro.setCategoryLevel3Id(Integer.valueOf(request.getParameter("categoryLevel3Id")));
			pro.setFileName(request.getParameter("fileName"));
			int res=ps.addNews(pro);
			if(res==1){
				out.print("true");
			}else{
				out.print("false");
			}
		}
		
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
